package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * spring容器启动监听器
 * 用来监听实现Redis数据初始化
 */
@Component
public class StrategyRedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService statisRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("---------开始进行vo攻略的Redis初始化------------");

        //从MySQL里获取所有的攻略明细数据
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            //这里判断一下,如果数据同步的时候发现Redis里面已经有这个key了就跳过,不然可能会造成数据丢失
            if (statisRedisService.isStrategyStatisVOEsixt(detail.getId())) {
                continue; //跳过此次循环
            }
            vo = new StrategyStatisVO();
            //遍历添加到Redis里
            try {
                BeanUtils.copyProperties(vo, detail);
                //不同的字段属性需要我们自己设置
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //设置vo到Redis里面
            statisRedisService.setStrategyStatisVO(vo);
        }
        System.out.println("---------vo攻略的Redis初始化结束------------");

        System.out.println("---------开始进行zset攻略的Redis初始化------------");
        List<StrategyStatisVO> vOs = statisRedisService.getStrategyStatisVOs(RedisKeys.STRATEGY_STATIS_VO.getPrefix());
        for (StrategyStatisVO detail : vOs) {
            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getStrategyId().toString());

            //需要判断一下zset里数据是否已经存在,若存在则跳过,不然会重复添加,导致数据错误
            if(statisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_COMMEND_SORT.getPrefix(),value)){
                continue;
            }
            //分数为收藏数加点赞数
            int scroes = detail.getFavornum() + detail.getThumbsupnum();
            //把分数加进zset里当前攻略的分数
            statisRedisService.addCommendScore(RedisKeys.STRATEGY_STATIS_VO.join(detail.getStrategyId().toString()), scroes);
        }

        for (StrategyStatisVO detail : vOs) {
            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getStrategyId().toString());

            //需要判断一下zset里数据是否已经存在,若存在则跳过,不然会重复添加,导致数据错误
            if(statisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),value)){
                continue;
            }
            //分数为阅读量加评论回复数
            int scroes = detail.getViewnum() + detail.getReplynum();
            //把分数加进zset里当前攻略的分数
            statisRedisService.addHotScroes(RedisKeys.STRATEGY_STATIS_VO.join(detail.getStrategyId().toString()), scroes);
        }
        System.out.println("---------zset攻略的Redis初始化结束------------");
    }
}
